package com.vshop.platform.system.crud.dao;

import cn.hutool.core.util.ArrayUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vshop.platform.system.domain.entity.SysOperLog;
import com.vshop.platform.system.crud.mapper.SysOperLogMapper;
import com.vshop.platform.system.domain.param.SysOperLogParam;
import org.springframework.stereotype.Repository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

import static com.vshop.platform.system.domain.entity.table.SysOperLogTableDef.SysOperLogTable;


/**
 * 操作日志记录 服务层实现。
 *
 * @author vshop
 * @since 2024-11-18
 */
@RequiredArgsConstructor
@Slf4j
@Repository
public class SysOperLogDao extends ServiceImpl<SysOperLogMapper, SysOperLog> {
    /**
     * 构建QueryWrapper查询条件
     *
     * @return 查询条件
     */
    public QueryWrapper buildQueryWrapper(SysOperLogParam param) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .and(SysOperLogTable.Title.like(param.getTitle()))
                .and(SysOperLogTable.BusinessType.eq(param.getBusinessType()))
                .and(SysOperLogTable.Status.eq(param.getStatus()))
                .and(SysOperLogTable.OperName.eq(param.getOperName()))
                .and(SysOperLogTable.OperIp.like(param.getOperIp()))
                .and(SysOperLogTable.OperTime.between(param.getParams().get("beginTime"),
                        param.getParams().get("endTime")))
                .orderBy(SysOperLogTable.Id.desc());
        if (ArrayUtil.isNotEmpty(param.getBusinessTypes())) {
            queryWrapper.and(SysOperLogTable.BusinessType.in(Arrays.asList(param.getBusinessTypes())));
        }
        return queryWrapper;
    }

}
